package com.haixiaoke.smartLock.haixiaoke;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Dell
 * @Date 2023/9/15
 **/
@Slf4j
@Service
public class SmartLockHxkCallBackService {

    @Autowired
    private SmartLockHxkLogEventHandleService smartLockHxkLogEventHandleService;

    public void devices(Map<String, Object> map) {
        log.info("嗨小克设备上线回调参数：{}"+map);
    }

    public Map<String,Object> deviceOpenRecords(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","操作成功");
        log.info("嗨小克设备开门回调参数：{}",map);
        try {
            Object content = map.get("deviceOpenRecords");
            if (ObjectUtil.isNotEmpty(content)) {
                List<Map<String,Object>> list = Convert.convert(List.class, content);
                smartLockHxkLogEventHandleService.logEventHandle(list);
            }else {
                resultMap.put("code",1);
                resultMap.put("msg","数据获取失败");
            }
        }catch (Exception e){
            log.error("嗨小克, 智能锁开门回调事件异常" + e.getMessage(), e);
            resultMap.put("code",1);
            resultMap.put("msg","操作失败");
        }
        return resultMap;
    }

    public Map<String, Object> deviceTakePhotos(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","操作成功");
        log.info("嗨小克设备抓拍回调参数：{}",map);
        try {
            Object content = map.get("devicePhotos");
            if (ObjectUtil.isNotEmpty(content)) {
                List<Map<String,Object>> list = Convert.convert(List.class, content);
                smartLockHxkLogEventHandleService.warnEventHandle(list);
            }else {
                log.info("key获取的参数为空");
                resultMap.put("code",1);
                resultMap.put("msg","数据获取失败");
            }
        }catch (Exception e) {
            log.error("嗨小克, 智能抓拍锁回调事件异常" + e.getMessage(), e);
            resultMap.put("code",1);
            resultMap.put("msg","操作失败");
        }
        return resultMap;
    }
}
